package com.lxp.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxp.eduService.domain.EduChapter;
import com.lxp.eduService.domain.EduVideo;
import com.lxp.eduService.domain.vo.CourseInfo;
import com.lxp.eduService.domain.vo.chapter.ChapterVo;
import com.lxp.eduService.domain.vo.chapter.VideoVo;
import com.lxp.eduService.mapper.EduChapterMapper;
import com.lxp.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.servicebase.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2021-11-20
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoServiceImpl eduVideoService;
    //根据课程id 查询出章节 以及小节
    @Override
    public List<ChapterVo> getChapterVideo(String courseID) {
        //查询出所有的章节

        QueryWrapper<EduChapter> eduChapterQueryWrapper =new QueryWrapper<>();
        eduChapterQueryWrapper.eq("course_id",courseID);
        eduChapterQueryWrapper.orderByAsc("gmt_create");
        List<EduChapter> eduChapters = baseMapper.selectList(eduChapterQueryWrapper);
        //查询所有的小节
        QueryWrapper<EduVideo> wrapper =new QueryWrapper<>();
        wrapper.eq("course_id",courseID);
        wrapper.orderByAsc("gmt_create");
        List<EduVideo> eduVideos = eduVideoService.list(wrapper);
        //封装章节
        //返回最终结果的集合
        List<ChapterVo> chapterVos =new ArrayList<>();
        for (EduChapter eduChapter : eduChapters) {
            ChapterVo chapterVo =new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            chapterVos.add(chapterVo);
            //封装小节
            //封装小节的集合
            List<VideoVo> videoVos =new ArrayList<>();
            for (EduVideo eduVideo : eduVideos) {
                //判断chapter_id 与 chapter的id相同
                if(Objects.equals(eduVideo.getChapterId(),chapterVo.getId())){
                    VideoVo videoVo =new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setVideos(videoVos);
        }

        return chapterVos;
    }

    //根据id 删除章节(没存在小节的时候才可以删除)
    public boolean deleteById(String chapterId) {
        //查询章节下面是否存在小节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if (count !=0){
            //有小节
            throw new MyException(20001,"无法删除,章节下面存在小节");
        }
        //无小节可以删除
        int result = baseMapper.deleteById(chapterId);
        //判断是否删除成功
        return result>0;
    }
}
